/**
 * Created by chengming on 18/5/27.
 * version 1.0
 */
const BaseJsonAdapter = require('../base/BaseJsonAdapter');
const iconv = require("iconv-lite");
const vm = require('vm');
const request = require('request-promise');
const logger = require('../../conf/logger');
const querystring = require("querystring")
const urlencode = require("urlencode")

/**
 * 北京互金网
 * @type {module.Bjp2p}
 */
module.exports = class ShenJiaoSuo3005PostJson extends BaseJsonAdapter{

    async doRequest(){
        var form = {
          channelCode: ['listedNotice_disc'],
          pageNum: 1,
          pageSize: 30,
          stock: ['300058']
        }

        var formData = JSON.stringify(form);
        var contentLength = formData.length;

        var options = {
            headers:{
              'Accept': 'application/json, text/javascript, */*; q=0.01',
              'Content-Type': 'application/json',
              'Host': 'www.szse.cn',
              'Origin': 'http://www.szse.cn',
              'Referer': 'http://www.szse.cn/disclosure/listed/notice/index.html?stock=300058&name=%E8%93%9D%E8%89%B2%E5%85%89%E6%A0%87',
              'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
              'X-Request-Type': 'ajax',
              'X-Requested-With': 'XMLHttpRequest',
              'Content-Length': contentLength
            },
            // uri:'http://www.bjp2p.com.cn/supervise/queryNews4List',
            uri:'http://www.szse.cn/api/disc/announcement/annList?random=0.724143925962021',
            body:formData,
            method:"POST",
            encoding:null
        }
        let body = await request(options)
        return body;
    }


    async reqJson(){
		    const body = await this.doRequest();
        let decodeBody = iconv.decode(body, this.encoding).toString();
        let data = JSON.parse(decodeBody);
        let url = 'http://www.szse.cn/disclosure/listed/bulletinDetail/index.html?'
        let contents = [];
        for(let row of data.data) {
          contents.push({
            title: row['title'],
            href: url + row['id']
          });
        }
        console.log(contents)
        return {data:contents}
    }
}
